package com.wyp.didishige.activity.me;

import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.utils.StringUtils;

import java.util.Timer;
import java.util.TimerTask;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.ResetPasswordByCodeListener;

public class ResetPswActivity extends BaseActivity {
    public AppCompatEditText phoneNum;
    public AppCompatEditText password;
    public AppCompatEditText code;

    AppCompatButton sendCode;
    AppCompatButton resetPsw;
    Timer sendTimer;
    TimerTask sendTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_psw);
        actionBar=getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle("重置密码");

        init();
    }

    @Override
    protected void initParamter() {
        super.initParamter();
        sendTimer = new Timer();
    }
    class SmsTask extends TimerTask{

        @Override
        public void run() {
            second--;
            Message msg=new Message();
            msg.what=1;
            handler.sendMessage(msg);
        }
    }
    @Override
    protected void initWidget() {
        super.initWidget();
        phoneNum = (AppCompatEditText) findViewById(R.id.phoneNum);
        password = (AppCompatEditText) findViewById(R.id.passWord);
        code = (AppCompatEditText) findViewById(R.id.code);
        sendCode = (AppCompatButton) findViewById(R.id.sendCode);
        resetPsw = (AppCompatButton) findViewById(R.id.resetPsw);
    }

    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        phoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtils.isPhone(s)) {
                    sendCode.setEnabled(true);
                } else {
                    sendCode.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendPhoneCode();
            }
        });
        resetPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetPsw();
            }
        });
    }

    @Override
    protected void initWidgetState() {
        super.initWidgetState();
    }

    /**
     * 发送手机验证码
     */
    public void sendPhoneCode() {
        String phone = phoneNum.getText().toString();
        if (StringUtils.isNullOrEmpty(phone)) {
            ToastUtil.showShort(this, "请输入手机号码");
            return;
        }

        BmobSMS.requestSMSCode(this, phone, "师哥帐号注册模板", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {
                    sendCode.setEnabled(false);
                    ToastUtil.showShort(ResetPswActivity.this, "短信验证码已发送，请注意查收");
                    sendTask = new SmsTask();
                    sendTimer.schedule(sendTask, 1000, 1000);
                } else {
                    ToastUtil.showShort(ResetPswActivity.this, String.format("短信发送失败，%s，请稍候再试", e.getLocalizedMessage()));
                }
            }
        });
    }

    private void resetPsw() {
        String smsCode = code.getText().toString();
        String newPsw = password.getText().toString();
        if (StringUtils.isNullOrEmpty(smsCode)) {
            ToastUtil.showShort(this, "输入验证码");
            return;
        }

        if (StringUtils.isNullOrEmpty(newPsw)) {
            ToastUtil.showShort(this, "输入密码");
            return;
        }

        if (!StringUtils.isValidPsw(newPsw)) {
            ToastUtil.showShort(this, "密码格式不正确");
            return;
        }

        BmobUser.resetPasswordBySMSCode(this, smsCode, newPsw, new ResetPasswordByCodeListener() {

            @Override
            public void done(BmobException ex) {
                if (ex == null) {
                    Log.i("smile", "密码重置成功");
                    ToastUtil.showShort(ResetPswActivity.this, "密码重置成功");
                    finish();
                } else {
                    if (ex.getErrorCode()==207){
                        ToastUtil.showShort(ResetPswActivity.this, "验证码错误");
                    }else{
                        ToastUtil.showShort(ResetPswActivity.this, "重置失败：code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
                    }

                    Log.i("smile", "重置失败：code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
                }
            }
        });
    }

    int second = 60;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (second <= 0) {
                        second = 60;
                        sendCode.setText("发送");
                        sendCode.setEnabled(true);
                        if (sendTask!=null){
                            sendTask.cancel();
                        }
                    } else {
                        sendCode.setText(String.format("%s秒后", second));
                    }
                    break;
            }
            return false;
        }
    });
}
